#ifndef __STYLUS_CONTROLS_H__
#define __STYLUS_CONTROLS_H__

// Includes
#include <PA9.h>       // Include for PA_Lib


#include "Avatar.h"
#include "Init.h"
#include "DPadControls.h"
#include "SeamlessMap.h"
#include "Spawn.h"

void StylusCtrl()
{
/********** STYLUS ACTIONS ************/
//------------------------------------------------------------
	 
	if(Stylus.Newpress) 
		{	
			PA_Clear8bitBg(0); // Reset the screen when we start a new character
			
			//save stylus start position
			styStartX = Stylus.X;
			styStartY = Stylus.Y;
		}
	   
		//PA_8bitDraw(0, 1);  //draws stylus movement on screen
	   	  
			   
		char letter[2]; letter[1] = 0;
		letter[0] = PA_CheckLetter(); // Returns the letter !!!
		
		if (Stylus.Released){ // only show if released
			//PA_OutputText(1, 2, 8, "Shape Recognized : %s", letter); // Letter recognized
		}		
		
		//PA_OutputText(1, 0, 10, "Shape : %s", PA_RecoShape); // Write the shape string
		switch(letter[0])
		{
//DASH TO THE RIGHT			
			case 'a':
				//Make sure player has enough stamina to do dash
				//make sure stylus movement is not caused by touching a button
				if((player.staminaCheck(dashCost)) && ((buttonTime + buttonRelease) <= Tick(gameClock)))
				{
					PauseTimer(staminaClock); //pause stamina clock until dash is finished
					dash = true;
					
					//if flag is not turned off, sprite will revert to paused animation frame
					moving = false; //turn off flag
					AS_SoundQuickPlay(swoosh);
					//move sprite while playing animation
					for(counter = 0; counter < dashCycle; counter++)
					{
						//dash to the right
						//PA_StartSpriteAnim(0, avSprNo, 53, 54, 7);
						PA_SetSpriteAnim(0, avSprNo, 53);
						x += dashDist;
						y += 0;
						//PA_SetSpriteXY(0, avSprNo, x, y);		//move sprite
						if(!(CheckCol(50,60)))
						{
							miniMap();
							mapScrolling();
							loadMap();
							spawnControl();
						}
						else //otherwise, reset x
						{
							x -= dashDist;
						}
						PA_WaitForVBL();
					}
					
					PA_StopSpriteAnim (0, avSprNo); //stop sprite animation
					PA_SetSpriteAnim(0, avSprNo, 0); //set animation frame to facing right
					
					UnpauseTimer(staminaClock); //unpause clock
				}
				break;
//DASH TO THE LEFT
			case 'b':
				//Make sure player has enough stamina to do dash
				//make sure stylus movement is not caused by touching a button
				if((player.staminaCheck(dashCost)) && ((buttonTime + buttonRelease) <= Tick(gameClock)))
				{
					PauseTimer(staminaClock); //pause stamina clock until dash is finished
					dash = true;
					
					//if flag is not turned off, sprite will revert to paused animation frame
					moving = false; //turn off flag
					AS_SoundQuickPlay(swoosh);
					PA_SetSpriteHflip(0, avSprNo, 1);
					//move sprite while playing animation
					for(counter = 0; counter < dashCycle; counter++)
					{
						//dash to the right
						//PA_StartSpriteAnim(0, avSprNo, 53, 54, 7);
						PA_SetSpriteAnim(0, avSprNo, 53);
						x -= dashDist;
						y += 0;
						//PA_SetSpriteXY(0, avSprNo, x, y);		//move sprite
						//if no collision, scroll map accordingly
						if(!(CheckCol(16,60)))
						{
							miniMap();
							mapScrolling();
							loadMap();
							spawnControl();
						}
						else //otherwise, reset x
						{
							x += dashDist;
						}
						PA_WaitForVBL();
					}
					PA_SetSpriteHflip(0, avSprNo, 0);
					
					PA_StopSpriteAnim (0, avSprNo); //stop sprite animation
					PA_SetSpriteAnim(0, avSprNo, 7); //set animation frame to facing
					
					UnpauseTimer(staminaClock); //unpause clock
				}
				break;
//JUMP (NoT YeT iMpLeMeNtEd)
			case 'c':
				//this feature has been cut for now
				break;
//GUARD
			case 'd':
				//Make sure player has enough stamina to do dash
				//make sure stylus movement is not caused by touching a button
				if((player.staminaCheck(guardCost)) && ((buttonTime + buttonRelease) <= Tick(gameClock)))
				{
					PauseTimer(staminaClock); //pause stamina clock until guard is finished
				
					//if flag is not turned off, sprite will revert to paused animation frame
					moving = false; //turn off flag
					
					
					PA_StopSpriteAnim (0, avSprNo); //stop sprite animation

					if(styStartX < (PA_GetSpriteX(0, avSprNo) + (PA_GetSpriteLx(0, avSprNo)/2)))
					//if stylus slash starts to the left of sprite, guard to the left
					//add half the sprite's length so that the attack is based off the sprites center
					{
						//flag used for combat
						guardL = true; //turn on left guard
						
						PA_SetSpriteAnim(0, avSprNo, 52); //set guard (left) frame
						
						//use for loop as a timer
						for(counter = 0; counter < guardCycle; counter++)
						{
							//do nothing
							PA_WaitForVBL();
						}
					
					PA_StopSpriteAnim (0, avSprNo); //stop sprite animation
					PA_SetSpriteAnim(0, avSprNo, 7); //set animation frame
					}
					else
					//otherwise guard to the right
					{
						guardR = true; //turn on right guard
						
						PA_SetSpriteAnim(0, avSprNo, 51);
						
						//use for loop as a timer
						for(counter = 0; counter < guardCycle; counter++)
						{
							//do nothing
							PA_WaitForVBL();
						}
						
					PA_StopSpriteAnim (0, avSprNo); //stop sprite animation
					PA_SetSpriteAnim(0, avSprNo, 0); //set animation frame
					}
					
					guardL = false; //reset flags
					guardR = false;
					
					UnpauseTimer(staminaClock); //unpause clock
				}
				break;
//KICK
			case 'e':
				if((player.staminaCheck(kickCost)) && ((buttonTime + buttonRelease) <= Tick(gameClock)))
				{
					PauseTimer(staminaClock); //pause stamina clock until dash is finished
					//if flag is not turned off, sprite will revert to paused animation frame
					moving = false; //turn off flag
				
					//check to see if the L or R triggers are being held
					if(!((Pad.Held.L)||(Pad.Held.R)))
					{
						AS_SoundQuickPlay(swoosh);//sound effect
						
						if(styStartX < (PA_GetSpriteX(0, avSprNo) + (PA_GetSpriteLx(0, avSprNo)/2)))
						//if stylus slash starts to the left of sprite, kick to the left
						//add half the sprite's length so that the attack is based off the sprites center
						{
							//make sprite run to location of slash before kicking
							//only check horizontal distance (vertical distance is ignored)
							if((spriteX + sprStartX) > (spriteX  + styStartX))
							{ 
								kickRunDist = (spriteX + sprStartX) - (spriteX  + styStartX);
								
								PA_StartSpriteAnim(0, avSprNo, 8, 13, 7); //run until location is reached
								
								while(kickRunDist > 0)
								{
									x-=2;
									y = y;
									kickRunDist -= 2;
									//if no collision, scroll map accordingly
									if(!(CheckCol(16,60)))
									{
										miniMap();
										mapScrolling();
										loadMap();
										spawnControl();
									}
									else //otherwise, reset x
									{
										x += 2;
										kickRunDist = 0;
									}
									PA_WaitForVBL();
								}
								kickRunDist = 0;
							}	
						
							monKick = true;
							//PA_StartSpriteAnim(0, avSprNo, 40, 50, 7);
							kickCount++; //increment kickCount
							if(kickCount == 1) //do first kick
							{
								PA_StopSpriteAnim (0, avSprNo); //stop any previous animation
								PA_StartSpriteAnimEx(0, avSprNo, 40, 43, 7, 1, 1);
								isKicked = true; //set flag to true
								kickNum = 1; //set count number
								ResetTimer(kickClock); //reset kick clock
							}
							else if (kickCount == 2) //do second kick
							{
								if(isKicked == true) //if kick already in progress
								{
									kickStart = PA_GetSpriteAnimFrame(0, avSprNo);
									if(kickStart < 40)
										kickStart = 44;
								}
								else //use default start
									kickStart = 44;
								PA_StopSpriteAnim (0, avSprNo); //stop any previous animation
								PA_StartSpriteAnimEx(0, avSprNo, kickStart, 47, 7, 1, 1);
								isKicked = true;
								kickNum = 2;
								ResetTimer(kickClock);
							}
							else //kickCount == 3, do third kick
							{
								if(isKicked == true) //if kick already in progress
								{
									kickStart = PA_GetSpriteAnimFrame(0, avSprNo);
									if(kickStart < 40)
										kickStart = 48;
								}
								else //use default start
									kickStart = 48;
								PA_StopSpriteAnim (0, avSprNo); //stop any previous animation
								PA_StartSpriteAnimEx(0, avSprNo, kickStart, 50, 7, 1, 1);
								isKicked = true;
								kickNum = 3;
								ResetTimer(kickClock);
								kickCount = 0; //kickCount = 3, so reset
							}
							
						}
						else
						//otherwise kick to the right
						{
							//make sprite run to location of slash before kicking
							//only check horizontal distance (vertical distance is ignored)
							if((spriteX + sprStartX) < (spriteX  + styStartX))
							{ 
								kickRunDist = (spriteX  + styStartX) - (spriteX + sprStartX + PA_GetSpriteLx(0, avSprNo));
								
								PA_StartSpriteAnim(0, avSprNo, 1, 6, 7); //run until location is reached
								
								while(kickRunDist > 0)
								{
									x+=2;
									
									y = y;
									kickRunDist -= 2;
									//if no collision, scroll map accordingly
									if(!(CheckCol(48,60)))
									{
										miniMap();
										mapScrolling();
										loadMap();
										spawnControl();
									}
									else //otherwise, reset x
									{
										x -= 2;//dashDist;
										kickRunDist = 0;
									}
									PA_WaitForVBL();
								}
								kickRunDist = 0;
							}
						
							monKick = true;
							kickCount++; //increment kickCount
							if(kickCount == 1) //do first kick
							{
								PA_StopSpriteAnim (0, avSprNo); //stop any previous animation
								PA_StartSpriteAnimEx(0, avSprNo, 29, 32, 7, 1, 1);
								isKicked = true; //set flag to true
								kickNum = 1; //set count number
								ResetTimer(kickClock); //reset kick clock
							}
							else if (kickCount == 2) //do second kick
							{
								if(isKicked == true) //if kick already in progress
								{
									kickStart = PA_GetSpriteAnimFrame(0, avSprNo);
									if(kickStart < 29)
										kickStart = 33;
								}
								else //use default start
									kickStart = 33;
								PA_StopSpriteAnim (0, avSprNo); //stop any previous animation
								PA_StartSpriteAnimEx(0, avSprNo, kickStart, 36, 7, 1, 1);
								isKicked = true;
								kickNum = 2;
								ResetTimer(kickClock);
							}
							else //kickCount == 3, do third kick
							{
								if(isKicked == true) //if kick already in progress
								{	
									kickStart = PA_GetSpriteAnimFrame(0, avSprNo);
									if(kickStart < 29)
										kickStart = 37;
								}
								else //use default start
									kickStart = 37;
								PA_StopSpriteAnim (0, avSprNo); //stop any previous animation
								PA_StartSpriteAnimEx(0, avSprNo, kickStart, 39, 7, 1, 1);
								isKicked = true;
								kickNum = 3;
								ResetTimer(kickClock);
								kickCount = 0; //kickCount = 3, so reset
							}
							
						}
					}
					else
					{
					//special
					PA_OutputSimpleText(1,4,22, "SPECIAL KICK!");
					}
					
					UnpauseTimer(staminaClock); //unpause clock
				}
				
				break;
//SHOOT
			case 'f':
				//make sure its really a tap AND sprite is not touched
				//make sure stylus movement is not caused by touching a button
				if((PA_Distance(styStartX, styStartY, Stylus.X, Stylus.Y) <= adjShotDist) && (touchSprite != true)
						&& ((buttonTime + buttonRelease) <= Tick(gameClock)))
				{
					//only shoot if tap is within vertical range/limit
					if((styStartY < (PA_GetSpriteY(0, avSprNo) + PA_GetSpriteLy(0, avSprNo) + vertShotLimit)) 
								&& (styStartY > (PA_GetSpriteY(0, avSprNo) - vertShotLimit)))
					{
						moving = false;
						
						//check to see if the L or R triggers are being held
						if(!((Pad.Held.L)||(Pad.Held.R)))
						{
						//not special
						
							AS_SoundQuickPlay(gun); // gunshot sound
							if(Stylus.X < (PA_GetSpriteX(0, avSprNo) + (PA_GetSpriteLx(0, avSprNo)/2)))
							//if stylus tap is to the left of sprite, shoot to the left
							{
								if((shotTime + shotNext) <= Tick(gameClock))
								//downtime between shots
								{
									PA_StartSpriteAnimEx(0, avSprNo, 25, 28, 7, 1, 1);
									shotTime = Tick(gameClock);
									isShot = true; //set flag
								}
							}
							else
							//otherwise shoot to the right
							{
								if((shotTime + shotNext) <= Tick(gameClock))
								//downtime between shots
								{
									PA_StartSpriteAnimEx(0, avSprNo, 21, 24, 7, 1, 1);
									shotTime = Tick(gameClock);
									isShot = true; //set flag
								}
							}
						}
						else
						{
						//special
						PA_StartSpriteAnimEx(0, avSprNo, 62, 84, 7, 1, 1);
						/*
						PA_OutputSimpleText(1,4,22, "SPECIAL SHOOT!");
						PA_LoadSpritePal(0, 1, (void*)Blast1_Pal);
						PA_LoadSpritePal(0, 2, (void*)Blast2_Pal);
						PA_LoadSpritePal(0, 3, (void*)Blast3_Pal);
						PA_LoadSpritePal(0, 4, (void*)Blast4_Pal);
						
						PA_CreateSprite(0, 1, (void*)BlastAnim0164x1472_Sprite, OBJ_SIZE_64X64, 1, 2, x, y);
						PA_CreateSprite(0, 2, (void*)BlastAnim0264x1472_Sprite, OBJ_SIZE_64X64, 1, 2, x + 64, y);
						PA_CreateSprite(0, 3, (void*)BlastAnim0363x1472_Sprite, OBJ_SIZE_64X64, 1, 2, x + 128, y);
						PA_CreateSprite(0, 4, (void*)BlastAnim0464x1472_Sprite, OBJ_SIZE_64X64, 1, 2, x + 192, y);
						
						PA_StartSpriteAnim(0, avSprNo, 62, 84, 7);
						PA_StartSpriteAnim(0, 1, 0, 22, 7);
						PA_StartSpriteAnim(0, 2, 0, 22, 7);
						PA_StartSpriteAnim(0, 3, 0, 22, 7);
						PA_StartSpriteAnim(0, 4, 0, 22, 7);
						*/
						
						}
					}
				}
				break;
			default:
				break;
		}
		
	
	
	//if(kickCycle == 0)
		//{
		//PA_SpriteAnimPause(0, avSprNo, 1);
		//PA_SetSpriteAnimFrame(0, avSprNo, 0);
		//}
	//kick clean-up
	/*
	if ((kickTime + kickInc) <= Tick(gameClock))
	{
		kickNum = 0;
		letter[0] = '0';
		PA_SpriteAnimPause(0, avSprNo, 1);
		PA_SetSpriteAnimFrame(0, avSprNo, 0);
		
	}
	if((PA_GetSpriteAnimFrame(0, avSprNo) == 32) && (kickNum != 1))
	{
		PA_SpriteAnimPause(0, avSprNo, 1);
		PA_SetSpriteAnimFrame(0, avSprNo, 0);
	}
	if((PA_GetSpriteAnimFrame(0, avSprNo) == 36) && (kickNum != 2))
	{
		PA_SpriteAnimPause(0, avSprNo, 1);
		PA_SetSpriteAnimFrame(0, avSprNo, 0);
	}
	if(PA_GetSpriteAnimFrame(0, avSprNo) == 39)
	{
		PA_SpriteAnimPause(0, avSprNo, 1);
		PA_SetSpriteAnimFrame(0, avSprNo, 0);
	}
	*/
}
	

#endif  //__STYLUS_CONTROLS_H__
